import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/Index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index',
    component: Index,
    children: [
      {
        path: '/index',
        name: 'Index',
        redirect: 'index/recommend',
        component: () => import(/* webpackChunkName: "index" */ '../components/Index/Index'),
        children: [
          {
            path: 'recommend',
            name: 'Recommend',
            component: () => import(/* webpackChunkName: "recommend" */ '../components/Index/Recommend')
          },
          {
            path: 'phone',
            name: 'Phone',
            component: () => import(/* webpackChunkName: "phone" */ '../components/Index/Phone')
          },
          {
            path: 'flat',
            name: 'Flat',
            component: () => import(/* webpackChunkName: "flat" */ '../components/Index/Flat')
          },
          {
            path: 'laptop',
            name: 'Laptop',
            component: () => import(/* webpackChunkName: "laptop" */ '../components/Index/Laptop')
          },
          {
            path: 'desktop',
            name: 'Desktop',
            component: () => import(/* webpackChunkName: "desktop" */ '../components/Index/Desktop')
          },
        ]

      },
      // 换新页面
      {
        path: 'renewindex',
        redirect: 'renewindex/renew',
        component: () => import(/* webpackChunkName: "renewindex" */ '../components/Renew/Index'),
        children: [
          {
            path: 'renew',
            component: () => import(/* webpackChunkName: "Renew" */ '../components/Renew/Renew'),
          },
          {
            // 依旧换新列表
            path: 'renewlist/:lid',
            name: 'Renewlist',
            component: () => import(/* webpackChunkName: "renewlist" */ '../components/Renew/List'),
            children: [

            ]
          }
        ]
      },
      // 回收栏目
      {
        path: 'tuijian',
        name: 'Tuijian',
        component: () => import(/* webpackChunkName: "tuijian" */ '../components/Recovery/Tuijian'),
      },
      // 关于我
      {
        path: '/aboutme',
        name: 'AboutMe',
        component: () => import(/* webpackChunkName: "AboutMe" */ '../components/AboutMe/AboutMe'),
      },
      //钱包
      {
        path: '/cash',
        name: 'Cash',
        component: () => import(/* webpackChunkName: "Cash" */ '../components/AboutMe/Cash'),
      },
      // 券包
      {
        path: '/coupon',
        name: 'Coupon',
        component: () => import(/* webpackChunkName: "Coupon" */ '../components/AboutMe/Coupon'),
      }

    ]
  },

  // 评估选项
  {
    path: '/prospecs',
    name: 'ProSpecs',
    component: () => import(/* webpackChunkName: "prospecs" */ '../components/Recovery/ProSpecs'),
  },
  // 订单
  {
    path: '/order',
    name: 'Order',
    // 路由拦截
    beforeEnter: (to, from, next) => {
      if (!sessionStorage.getItem("username")) {
        if (to.path !== '/login') {
          return next('/login')
        }
      }
      next()
    },
    component: () => import(/* webpackChunkName: "Order" */ '../components/Recovery/Order'),
  },
  {
    path: '/orderlist',
    name: 'OrderList',
    // 路由拦截
    beforeEnter: (to, from, next) => {
      if (!sessionStorage.getItem("username")) {
        if (to.path !== '/login') {
          return next('/login')
        }
      }
      next()
    },
    component: () => import(/* webpackChunkName: "OrderList" */ '../components/AboutMe/OrderList'),
  },
  {
    path: '/orderdetails/:orderid',
    name: 'OrderDetails',
    props: true,
    // 路由拦截
    beforeEnter: (to, from, next) => {
      if (!sessionStorage.getItem("username")) {
        if (to.path !== '/login') {
          return next('/login')
        }
      }
      next()
    },
    component: () => import(/* webpackChunkName: "OrderDetails" */ '../components/AboutMe/OrderDetails'),
  },
  // 登录
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "Login" */ '../views/Login.vue'),
  },
  // 注册
  {
    path: '/register',
    name: 'Register',
    component: () => import(/* webpackChunkName: "Register" */ '../views/Register'),
  },
  // 商品详情
  {
    path: '/prodetail/:protitle/:proprice',
    props: true,
    name: 'proDetail',
    component: () => import(/* webpackChunkName: "proDetail" */ '../components/Renew/proDetail')
  },
  // 商品详情添加旧机(选择选项)
  {
    path: '/addold',
    name: 'addold',
    component: () => import(/* webpackChunkName: "addold" */ '../components/Renew/AddOldMachine')
  },
  // 选择旧机
  {
    path: '/oldpro',
    name: 'Oldpro',
    component: () => import(/* webpackChunkName: "Oldpro" */ '../components/Renew/Oldpro')
  },
  //二手
  {
    path: '/Assistant',
    name: 'Assistant',
    component: () => import(/* webpackChunkName: "Assistant" */ '../components/Index/Assistant.vue'),
  },
  //常见问题
  {
    path: '/question',
    name: 'Question',
    component: () => import(/* webpackChunkName: "Question" */ '../components/AboutMe/Question.vue')
  },
  // 搜索
  {
    path: '/search',
    name: 'Search',
    component: () => import(/* webpackChunkName: "Search" */ '../views/Search')
  },
]
// 重复点击同一个导航不报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
  routes
})

export default router
